#include "bsp_ili2107.h"
#include "stm32f1xx_hal.h"

#define ILI2107_ADDR    0x82

extern I2C_HandleTypeDef hi2c1;

int Bsp_IliReadFwVersion(uint32_t *version)
{
    uint8_t reg = 0x40;
    uint8_t *pBuf = (uint8_t *)version;
    
	//HAL_I2C_Master_Transmit(&hi2c1, ILI2107_ADDR, &reg, 1, 100);
    //HAL_I2C_Master_Receive(&hi2c1, ILI2107_ADDR, pBuf, 3, 100);
    
    HAL_I2C_Mem_Read(&hi2c1, ILI2107_ADDR, reg, 1, pBuf, 3, 100);
    
    return 0;
}

int Bsp_IliReadPoint(uint16_t *x1, uint16_t *y1, uint8_t *figer1, uint16_t *x2, uint16_t *y2, uint8_t *figer2)
{
    uint8_t buf[9];
    
    HAL_I2C_Mem_Read(&hi2c1, ILI2107_ADDR, 0x10, 1, buf, 9, 100);
    
    *figer1 = buf[0] & 0x01;
    *figer2 = (buf[0] & 0x02) >> 1;
    
    *x1 = (buf[2] << 8) | buf[1];
    *y1 = (buf[4] << 8) | buf[3];
    *x2 = (buf[6] << 8) | buf[5];
    *y2 = (buf[8] << 8) | buf[7];
}


int Bsp_IliReadPanelInfo(uint16_t *xMax, uint16_t *yMax, uint8_t *xChn, uint8_t *yChn)
{
    uint8_t buf[6];
    
    HAL_I2C_Mem_Read(&hi2c1, ILI2107_ADDR, 0x20, 1, buf, 6, 100);

    *xMax = (buf[1] << 8) | buf[0];
    *yMax = (buf[3] << 8) | buf[2];
    *xChn = buf[4];
    *yChn = buf[5];
}


int Bsp_IliEnterMassProductiveCalibration(void)
{
    HAL_I2C_Mem_Write(&hi2c1, ILI2107_ADDR, 0xCC, 1, NULL, 0, 200);
}

